﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;

namespace WcfContrib.Metadata
{
    /// <summary>
    /// Represents an operation policy definition
    /// </summary>
    public sealed class OperationPolicyDescription : PolicyDefinition<OperationDescription>
    {
        /// <summary>
        /// Gets the operation faults binding assertions.
        /// </summary>
        /// <value>The faults.</value>
        public Collection<PolicyDefinition<FaultDescription>> Faults { get; internal set; }

        /// <summary>
        /// Gets the operation messages binding assertions.
        /// </summary>
        /// <value>The messages.</value>
        public Collection<PolicyDefinition<MessageDescription>> Messages { get; internal set; }

        /// <summary>
        /// Gets the policies defined on a specific message description
        /// </summary>
        /// <param name="message">The message description.</param>
        /// <returns></returns>
        public PolicyDefinition<MessageDescription> GetMessagePolicies(MessageDescription message)
        {
            PolicyDefinition<MessageDescription> desc = null;

            foreach (var item in Messages)
            {
                if (item.Description.Equals(message))
                {
                    desc = item;

                    break;
                }
            }

            return desc;
        }

        /// <summary>
        /// Gets the policies defined on a specific fault description
        /// </summary>
        /// <param name="fault">The fault description.</param>
        /// <returns></returns>
        public PolicyDefinition<FaultDescription> GetFaultPolicies(FaultDescription fault)
        {
            PolicyDefinition<FaultDescription> desc = null;

            foreach (var item in Faults)
            {
                if (item.Description.Equals(fault))
                {
                    desc = item;

                    break;
                }
            }

            return desc;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OperationPolicyDescription"/> class.
        /// </summary>
        public OperationPolicyDescription()
        {
            Faults = new Collection<PolicyDefinition<FaultDescription>>();

            Messages = new Collection<PolicyDefinition<MessageDescription>>();
        }
    }
}
